<?php
/** 
 * Plugin 'rdt_i18n' for the 'ameos_formidable' extension.
 *
 * @author	Jerome Schneider <typo3dev@ameos.com>
 */


class tx_rdti18n extends formidable_mainrenderlet {

	var $aOButtons = array();

	function _render() {

		if(!$this->oForm->oDataHandler->i18n()) {
			$this->oForm->mayday("renderlet:I18N <b>'" . $this->_getName() . "'</b>: Datahandler has to declare <b>/i18n/use=true</b> for renderlet:I18N to work");
		}

		$aHtmlBag = array();

		$aCurData = $this->oForm->oDataHandler->_getListData();
		
		if(empty($aCurData)) {
			$aCurData = $this->oForm->oDataHandler->i18n_getStoredParent();
		}

		if(!empty($aCurData)) {

			$aLangs = $this->oForm->oDataHandler->getT3Languages();

			$iUid = $aCurData["uid"];
			$aChildRecords = $this->oForm->oDataHandler->i18n_getChildRecords($iUid);
			$aChildLanguages = array_keys($aChildRecords);

			$aHtml = array();
			reset($aLangs);
			$aKeys = array_keys($aLangs);
			while(list(, $iLangUid) = each($aKeys)) {

				if($iLangUid != $this->oForm->oDataHandler->i18n_getDefLangUid()) {

					if($this->oForm->__getEnvExecMode() !== "BE" || $GLOBALS["BE_USER"]->checkLanguageAccess($iLangUid)) {
						$aLang = $aLangs[$iLangUid];

						if(in_array($iLangUid, $aChildLanguages)) {
							$bExists = TRUE;
							$sEvent = <<<EVENT
							
							\$aParams = \$this->getUserObjParams();

							return \$this->majixRequestEdition(
								\$aParams["childrecords"][\$aParams["sys_language_uid"]]["uid"],
								\$this->oDataHandler->tablename()
							);
EVENT;
						} else {
							$bExists = FALSE;
							$sEvent = <<<EVENT
							
							\$aParams = \$this->getUserObjParams();

							return \$this->majixRequestNewI18n(
								\$this->oDataHandler->tablename(),
								\$aParams["record"]["uid"],
								\$aParams["sys_language_uid"]
							);
EVENT;
						}

						$aConf = array(
							"type" => "BUTTON",
							"label" => $aLang["title"] . ($bExists ? "" : " [NEW]"),
							"onclick-default" => array(
								"runat" => "client",
								"userobj" => array(
									"php" => $sEvent,
								)
							)
						);

						if(($aCustomConf = $this->_navConf("/stdbutton")) !== FALSE) {
							$aConf = t3lib_div::array_merge_recursive_overrule(
								$aConf,
								$aCustomConf
							);
						}
						
						$sName = $this->_getName() . "-record-" . $iUid . "-lang-" . $iLangUid;
						$aConf["name"] = $sName;

						$this->aOButtons[$sName] = $this->oForm->_makeRenderlet(
							$aConf,
							$this->sXPath . $sName. "/",
							FALSE,
							$this,
							FALSE,
							FALSE
						);

						$this->oForm->aORenderlets[$sName] =& $this->aOButtons[$sName];

						
						$iIndex = $this->oForm->pushForcedUserObjParam(
							array(
								"translation_exists" => $bExists,
								"sys_language_uid" => $iLangUid,
								"childrecords" => $aChildRecords,
								"childlanguages" => $aChildLanguages,
								"record" => $aCurData,
								"lang" => $aLang,
								"langs" => $aLangs,
							)
						);

						$aRendered = $this->aOButtons[$sName]->render();
						$aHtmlBag[] = $this->aOButtons[$sName]->wrap($aRendered["__compiled"]);
						
						$this->oForm->pullForcedUserObjParam($iIndex);
					}
				}
			}
		}

		return implode("", $aHtmlBag);
	}

	/*var $oBindedForm = FALSE;

	function _init(&$oForm, $aElement, $aObjectType, $sXPath) {
		parent::_init($oForm, $aElement, $aObjectType, $sXPath);

		if(($mBindsTo = $this->_navConf("/binds_to")) !== FALSE) {
			if($this->oForm->isRunneable($mBindsTo)) {	
				$this->oBindedForm = $this->callRunneable($mBindsTo);
			}
		}
	}*/

/*	function _render() {

		$aHtmlBag = array();

		$aListData = $this->oForm->oDataHandler->_getListData();

		if(!empty($aListData)) {

			$aLangs = $this->oForm->oDataHandler->getT3Languages();
			
			$iUid = $aListData[$this->oForm->oDataHandler->_navConf("/keyname")];
			$aChildRecords = $this->oForm->oDataHandler->i18n_getChildRecords($iUid);
			$aChildLanguages = array_keys($aChildRecords);

			$aHtml = array();
			reset($aLangs);

			while(list($iLangUid, $aLang) = each($aLangs)) {

				$sKey = "langs." . $aLang["uid"];

				$aHtmlBag[$sKey . ".title"] = $aLang["title"];
				$aHtmlBag[$sKey . ".sys_language_uid"] = $aLang["uid"];

				$aEvents = array();
				$sImgPath = $this->oForm->sExtRelPath . "res/images/flags/" . $aLang["flag"];

				if(in_array($iLangUid, $aChildLanguages)) {
					
					$bExists = TRUE;
					$aHtmlBag[$sKey . ".action"] = "edit";
					

					// le record existe dans cette langue
					$iRecordUid = $aChildRecords[$iLangUid][$this->oForm->oDataHandler->_navConf("/keyname")];
					$aHtmlBag[$sKey . ".record.uid"] = $iRecordUid;
					$aHtmlBag[$sKey . ".record.sys_language_uid"] = $aChildRecords[$iLangUid]["sys_language_uid"];
					$aHtmlBag[$sKey . ".record.l18n_parent"] = $aChildRecords[$iLangUid]["l18n_parent"];

					

					$sLink = $this->getLinkEdit($iRecordUid);

				} else {

					$bExists = FALSE;
					$aHtmlBag[$sKey . ".action"] = "create";
					$sLink = $this->getLinkNew(
						$iUid,
						$iLangUid
					);
				}

				$aFlag = $this->_getFlag($sImgPath, $bExists, $aLang);

				if($aFlag["type"] === "image") {
					$sFlag = "<img border=0 src='" . $aFlag["value"] . "' alt='" . htmlspecialchars($aLang["title"]) . "' title='" . htmlspecialchars($aLang["title"]) . "' />";
				} else {
					$sFlag = $aFlag["value"];
				}

				$aHtmlBag[$sKey . ".action.js"] = $sLink;
				$aHtmlBag[$sKey . ".action.wrap"] = "<a href=\"javascript:void(0);\" onclick='|'>";
				$aHtml[] = str_replace("|", $sLink, $aHtmlBag[$sKey . ".action.wrap"]) . $sFlag . "</a>";
			}

			$aHtmlBag["__compiled"] = implode(" ", $aHtml);

			ksort($aHtmlBag);
			//debug($aHtmlBag);
			return $aHtmlBag;
		}
	}*/

/*	function getLinkEdit($iUid) {
		
		if(is_object($this->oBindedForm)) {

			$aEvents = array();

			$aServerEvents = $this->_getProgServerEvents();

			$aEvents[] = $this->oForm->oRenderer->_getServerEvent(
				$this->_getName(),
				$aServerEvents["edition"],
				array(
					"uid" => $iUid
				)
			);

			return $this->oForm->oRenderer->wrapEventsForInlineJs($aEvents);
		}

		return "";
	}

	function getLinkNew($iParentUid, $iLangUid) {

		if(is_object($this->oBindedForm)) {

			$aEvents = array();

			$aServerEvents = $this->_getProgServerEvents();
			$aEvents[] = $this->oForm->oRenderer->_getServerEvent(
				$this->_getName(),
				$aServerEvents["creation"],
				array(
					"parentuid" => $iParentUid,
					"languid" => $iLangUid
				)
			);

			return $this->oForm->oRenderer->wrapEventsForInlineJs($aEvents);
		}

		return "";
	}*/


	function _getFlag($sPath, $bExists, $aLang) {

		if(($aFlags = $this->_navConf("/flags")) !== FALSE) {
			//debug($aFlags);

			$aDefinition = FALSE;

			reset($aFlags);
			while(list(, $aFlag) = each($aFlags)) {
				if($aFlag["uid"] == $aLang["uid"]) {
					$aDefinition = $aFlag;
					break;
				}
			}

			if($aDefinition !== FALSE) {

				if($bExists === TRUE) {
					$aDefinition = $aDefinition["exists"];
				} else {
					$aDefinition = $aDefinition["dontexist"];
				}

				if(array_key_exists("path", $aDefinition)) {
					// on renvoie l'image

					if($this->oForm->isRunneable($aDefinition["path"])) {
						$aDefinition["path"] = $this->callRunneable($aDefinition["path"]);
					}

					return array(
						"type" => "image",
						"value" => $this->oForm->toWebPath($aDefinition["path"])
					);
				} elseif(array_key_exists("label", $aDefinition)) {
					// on renvoie le label

					if($this->oForm->isRunneable($aDefinition["label"])) {
						$aDefinition["label"] = $this->callRunneable($aDefinition["label"]);
					}

					return array(
						"type" => "text",
						"value" => $this->oForm->_getLLLabel($aDefinition["label"])
					);
				} else {
					/* on renvoie le flag par defaut */
				}
			} else {
				/* on renvoie le flag par defaut */
			}
		}

		if($bExists === TRUE) {
			
			$sTypoScript =<<<TYPOSCRIPT

	file = GIFBUILDER
	file {
		XY = [10.w], [10.h]
		
		10 = IMAGE
		10.file = {$sPath}
	}

TYPOSCRIPT;

		} else {

			$sTypoScript =<<<TYPOSCRIPT

	file = GIFBUILDER
	file {
		XY = [10.w], [10.h]
		
		10 = IMAGE
		10.file = {$sPath}

		15 = EFFECT
		15.value = gamma=4
	}

TYPOSCRIPT;

		}

		$this->oForm->_callUserObj(
			array(
				"userobj" => array(
					"ts" => $sTypoScript
				)
			),
			array(),
			FALSE,
			$this
		);

		return array(
			"type" => "image",
			"value" => $this->oForm->toWebPath(
				$this->oForm->cObj->IMG_RESOURCE(
					$this->oForm->aLastTs
				)
			)
		);
	}

	function _listable() {
		return $this->oForm->_defaultTrue("/listable/", $this->aElement) && $this->oForm->oDataHandler->i18n();
	}

	function _activeListable() {		// listable as an active HTML FORM field or not in the lister
		return $this->oForm->_defaultTrue("/activelistable/", $this->aElement);
	}

	function _renderonly() {
		return TRUE;
	}

	function cleanBeforeSession() {
		$this->aOButtons = array();
		$this->baseCleanBeforeSession();
	}
}


	if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/rdt_i18n/api/class.tx_rdti18n.php"])	{
		include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/rdt_i18n/api/class.tx_rdti18n.php"]);
	}
?>